#include <memory>
#include <map>
#include <string>
#include <functional>
#include <iostream>
// 基础产品类
class Product {
public:
    virtual void operation() = 0;
    virtual ~Product() {}
};

// 具体产品类
class ConcreteProductA : public Product {
public:
    void operation() override {
        std::cout << "ConcreteProductA operation" << std::endl;
    }
};

class ConcreteProductB : public Product {
public:
    void operation() override {
        std::cout << "ConcreteProductB operation" << std::endl;
    }
};

// 模板化的工厂类
class Factory {
private:
    std::map<std::string, std::function<Product*()>> registry;

public:
    template<typename T>
    void registerProduct(const std::string& key) {
        registry[key] = [] { return new T(); };
    }

    Product* create(const std::string& key) {
        if (registry.find(key) != registry.end()) {
            return registry[key]();
        }
        return nullptr;
    }
};

// 使用示例
int main() {
    Factory factory;
    
    // 注册产品类型
    factory.registerProduct<ConcreteProductA>("ProductA");
    factory.registerProduct<ConcreteProductB>("ProductB");

    // 创建产品对象
    Product* productA = factory.create("ProductA");
    Product* productB = factory.create("ProductB");

    // 使用产品
    productA->operation();
    productB->operation();

    delete productA;
    delete productB;
}